package view
{
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Endian;
	
	import models.ModelLocator;
	
	import utils.Coursor;
	
	public class Console extends Sprite
	{
		public var pointAry:Array;
		private var maxW:Number = 763;
		private var maxH:Number = 389;
		private var centerW:Number = 356;
		private var centerH:Number = 308;
		private var centerPoint:Point = new Point(maxW/2,maxH/2);
		private var first:ConsolePoint;
		private var second:ConsolePoint;
		private var thrid:ConsolePoint;
		private var fourth:ConsolePoint;
		private var target:ConsolePoint;
		private var type:String;
		public var bitmap:Bitmap = ModelLocator.getInstance().bitmap;
		private var coefficient:Number;
		public var change:Boolean = true;
		public var content:Sprite;
		private var _rec:Rectangle;
		
		private var orgX:int;
		private var orgY:int;
		private var orgW:int;
		private var orgH:int;
		
		
		private var lineShape:Shape = new Shape();
		public function Console()
		{
			content = new Sprite();
			this.addChild(content);
			addBitmap();
			content.addChild(lineShape);
			content.x = maxW/2;
			content.y = maxH/2;
			content.addEventListener(MouseEvent.MOUSE_DOWN,onContentMouseClick);
			addPoint()
			drawThis();
			drawLine();
			initmask();
		}
		private function addPoint():void{
			pointAry = new Array;
			first = new ConsolePoint(content,bitmap.x,bitmap.y,onClick,onMosueOver);
			second = new ConsolePoint(content,bitmap.x+bitmap.width,bitmap.y,onClick,onMosueOver);
			thrid = new ConsolePoint(content,bitmap.x+bitmap.width,bitmap.y+bitmap.height,onClick,onMosueOver);
			fourth = new ConsolePoint(content,bitmap.x,bitmap.y+bitmap.height,onClick,onMosueOver);
			pointAry.push(first,second,thrid,fourth);
		}
		private function addBitmap():void{
			var sw:Number = bitmap.width/centerW;
			var sh:Number = bitmap.height/centerH;
			var rate:Number;
			if(sw < sh){
				rate = centerH/bitmap.height;
			}else{
				rate = centerW/bitmap.width;
			}
			bitmap.scaleX = bitmap.scaleY = rate;
			content.addChild(bitmap);
			bitmap.x = - bitmap.width * 0.5;
			bitmap.y =  - bitmap.height * 0.5;
			coefficient = bitmap.width*0.5/rate;
			
			orgX = bitmap.x ;
			orgY = bitmap.y;
			orgW = bitmap.width;
			orgH = bitmap.height;
			
		}
		public function setRest():void{
			bitmap.x = orgX;
			bitmap.y = orgY;
			bitmap.width = orgW;
			bitmap.height = orgH;
			
			content.x = maxW/2;
			content.y = maxH/2;
			content.rotation = 0;
			
			first.setXY(bitmap.x,bitmap.y);
			second.setXY(bitmap.x+bitmap.width,bitmap.y);
			thrid.setXY(bitmap.x+bitmap.width,bitmap.y+bitmap.height);
			fourth.setXY(bitmap.x,bitmap.y+bitmap.height);
			drawLine();
			
		}
		public function set rec(value:Rectangle):void{
			this._rec = value;
			bitmapMask.graphics.clear();
			bitmapMask.graphics.beginFill(0xff0000);
			bitmapMask.graphics.drawRect(value.x,value.y,value.width,value.height)
			bitmapMask.graphics.endFill();
			bitmap.mask = bitmapMask;
		}
		private var bitmapMask:Shape;
		private function initmask():void{
			bitmapMask = new Shape()
			this.addChild(bitmapMask);
		}
		public function get rec():Rectangle{
			return _rec;
		}
		private function onMosueOver(event:MouseEvent):void{
			if(change == false){
				return;
			}
			var t:ConsolePoint = event.target as ConsolePoint; 
			if(Math.sqrt(t.mouseX*t.mouseX + t.mouseY*t.mouseY) < 4){
				if(t == first || t == thrid){
					Coursor.getInstance().showCoursor("left");
				}else if(t == second || t == fourth){
					Coursor.getInstance().showCoursor("right");
				}
				type = "scale";
			}else{
				type = "rotation";
				Coursor.getInstance().showCoursor("rotation");
			}
		}
		private function onContentMouseClick(event:MouseEvent):void{
			if(event.target is ConsolePoint){
				return;
			}
			type = "move";
			change = false;
			Coursor.getInstance().showCoursor("move");
			content.startDrag();
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		private function onClick(event:MouseEvent):void{
			change = false;
			target = event.target as ConsolePoint;
			this.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		private function onMouseMove(event:MouseEvent):void{
			if(type == "scale")
				runNewPoint();
			else if(type == "rotation")
				runRotation();
			drawLine();
		}
		private function onMouseUp(event:MouseEvent):void{
			this.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			Coursor.getInstance().removeCoursor();
			content.stopDrag();
			change = true;
		}
		private function runNewPoint():void{
			bitmap.scaleX = bitmap.scaleY = Math.abs(content.mouseX)/coefficient;
			bitmap.x = - bitmap.width * 0.5;
			bitmap.y =  - bitmap.height * 0.5;
			first.setXY(bitmap.x,bitmap.y);
			second.setXY(bitmap.x+bitmap.width,bitmap.y);
			thrid.setXY(bitmap.x+bitmap.width,bitmap.y+bitmap.height);
			fourth.setXY(bitmap.x,bitmap.y+bitmap.height);
		}
		private function runRotation():void{
			if(target == first || target == thrid){
				var angle:Number = Math.atan((this.mouseY-content.y)/(this.mouseX-content.x)) - Math.atan(bitmap.height/bitmap.width);
			}else if(target == second || target == fourth){
				angle = Math.atan((this.mouseY-content.y)/(this.mouseX-content.x)) + Math.atan(bitmap.height/bitmap.width);
			}
			
			content.rotation = angle * 180/Math.PI;
		}
		private function runPoint():void{
			var t:Boolean;
			if(target == first){
				second.y = first.y;
				second.x = 2 * centerPoint.x - first.x;
				fourth.x = first.x;
				fourth.y = 2 * centerPoint.y - first.y;
				thrid.x = second.x;
				thrid.y = fourth.y;
			}else if(target == second){
				first.y = second.y;
				first.x = 2 * centerPoint.x - second.x;
				thrid.x = second.x;
				thrid.y = 2 * centerPoint.y - second.y;
				fourth.x = first.x;
				fourth.y = thrid.y;
			}else if(target == thrid){
				second.x = thrid.x;
				second.y = 2 * centerPoint.y - thrid.y;
				fourth.y = thrid.y;
				fourth.x = 2 * centerPoint.x - thrid.x;
				first.x = fourth.x;
				first.y = second.y;
			}else if(target == fourth){
				first.x = fourth.x;
				first.y = 2 * centerPoint.y - fourth.y;
				thrid.y = fourth.y;
				thrid.x = 2 * centerPoint.x - fourth.x;
				second.x = thrid.x;
				second.y = first.y;
			}
			target.x = this.mouseX;
			target.y = this.mouseY;
		}
		private function drawThis():void{
			graphics.beginFill(0x666666,0);
			graphics.drawRect(0,0,763,389);
			graphics.endFill();
		}
		private function drawLine():void{
			
			lineShape.graphics.clear();
			lineShape.graphics.lineStyle(1,0x000000);
			lineShape.graphics.moveTo(first.x,first.y);
			lineShape.graphics.lineTo(second.x,second.y);
			lineShape.graphics.lineTo(thrid.x,thrid.y);
			lineShape.graphics.lineTo(fourth.x,fourth.y);
			lineShape.graphics.lineTo(first.x,first.y);
			
		}
		
	}
}